package model;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.HtmlNode;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.TagNodeVisitor;

import android.graphics.Bitmap;

public class GlobalData {
	private String[] name = new String[9];
	private String[] tel = new String[9];
	private String[] dates = new String[9];
	
	private String[] conso_voix = new String[9];
	private String[] conso_voix_h = new String[9];
	private String[] conso_voix_num_spec = new String[9];
	private String[] conso_inter_voix = new String[9];
	private String[] conso_inter_voix_h = new String[9];
	
	private String[] conso_sms = new String[9];
	private String[] conso_sms_h = new String[9];
	private String[] conso_inter_sms = new String[9];
	private String[] conso_inter_sms_h = new String[9];
	
	private String[] conso_mms = new String[9];
	private String[] conso_mms_h = new String[9];
	private String[] conso_inter_mms = new String[9];
	private String[] conso_inter_mms_h = new String[9];
	
	private String[] conso_data = new String[9];
	private String[] conso_data_h = new String[9];
	private String[] conso_inter_data = new String[9];
	private String[] conso_inter_data_h = new String[9];
	
	private String[] total_forfait = new String[9];
	private String[] total_forfait_h = new String[9];
	private String[] total = new String[9];
	
	private LinkedList<String> imagesString = new LinkedList<String>();
	private LinkedList<Bitmap> imagesBitmap = new LinkedList<Bitmap>();
	private ArrayList<String> factures = new ArrayList<String>();
	private ArrayList<String> facturesCost = new ArrayList<String>();
	
	public GlobalData() {
	}
	
	public void parseAuth(String data) {
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(data);
		
		node.traverse(new TagNodeVisitor() {
			public boolean visit(TagNode arg0, HtmlNode htmlNode) {
		        if (htmlNode instanceof TagNode) {
		            TagNode tag = (TagNode) htmlNode;
		            if ("img".equals(tag.getName()) && tag.getAttributeByName("onmouseover")!=null) {
		            	imagesString.add("https://mobile.free.fr/moncompte/"+tag.getAttributeByName("src"));
		            }
		        }
				return true;
			}
		});
	}
	
	public void parseData(String data) {
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(data);
		
		node.traverse(new TagNodeVisitor() {
			int cmpt_NumAccount = 0;
			int cmpt = 0;
			int cmpt_div = 0;
			Pattern p = Pattern.compile("(.*) - .* - ([0-9]+)");
			public boolean visit(TagNode arg0, HtmlNode htmlNode) {
		        if (htmlNode instanceof TagNode) {
		            TagNode tag = (TagNode) htmlNode;
		            if ("div".equals(tag.getName())) {
		            	Matcher m = p.matcher(tag.getText().toString());
		            	if(m.find()) {
			            	//name = m.group(1)+" - "+m.group(2); 
		            		//System.out.println("NAME LINE ->"+m.group(1).trim()+" "+m.group(2).trim());
		            		setName(m.group(1).trim(), cmpt_NumAccount); 
		            		setTel(m.group(2).trim(), cmpt_NumAccount);
		            	}
		            	if(cmpt_div==10) {
		            		String dat = tag.getText().toString().replace("Vos consommations", "Conso").trim();
		            		setDates(dat.substring(0, 33), cmpt_NumAccount);
		            	}
		            	cmpt_div++;
		            }
		            if ("tr".equals(tag.getName())) {
		            	if(tag.getAttributeByName("class").contains("Facture")) {
		            		//System.out.println("DEBUG LINE FACTURES ->"+tag.getText().toString().trim());
		            		String factdate = tag.getText().toString().trim().split("\n")[0].split(":")[1].trim();
		            		String factcost = tag.getText().toString().trim().split("\n")[3].split(":")[1].trim();
		            		//System.out.println("DEBUG BINFO FACTURES ->"+factdate + " "+factcost);
		            		factures.add(factdate.substring(6, 10)+factdate.substring(3, 5)+factdate.substring(0, 2));
			            	facturesCost.add(factcost);
		            	}
		            }
		            if ("span".equals(tag.getName())) {
		            	//System.out.println("DEBUG LINE ->"+tag.getText().toString()+" "+cmpt);
		            	if(cmpt==3 + 35 * cmpt_NumAccount)
		            		conso_voix[cmpt_NumAccount] = tag.getText().toString();
		            	if(cmpt==5 + 35 * cmpt_NumAccount)
		            		conso_voix_h[cmpt_NumAccount] = tag.getText().toString();
		            	if(cmpt==8 + 35 * cmpt_NumAccount)
		            		conso_voix_num_spec[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==10 + 35 * cmpt_NumAccount)
		            		conso_sms[cmpt_NumAccount] = tag.getText().toString();
		            	if(cmpt==12 + 35 * cmpt_NumAccount)
		            		conso_sms_h[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==14 + 35 * cmpt_NumAccount)
		            		conso_mms[cmpt_NumAccount] = tag.getText().toString();
		            	if(cmpt==15 + 35 * cmpt_NumAccount)
		            		conso_mms_h[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==17 + 35 * cmpt_NumAccount)
		            		conso_data[cmpt_NumAccount] = tag.getText().toString();
		            	if(cmpt==18 + 35 * cmpt_NumAccount)
		            		conso_data_h[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==20 + 35 * cmpt_NumAccount)
		            		conso_inter_voix[cmpt_NumAccount] = tag.getText().toString();
		            	if(cmpt==22 + 35 * cmpt_NumAccount)
		            		conso_inter_voix_h[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==25 + 35 * cmpt_NumAccount)
		            		conso_inter_sms[cmpt_NumAccount] = tag.getText().toString()+" SMS";
		            	if(cmpt==26 + 35 * cmpt_NumAccount)
		            		conso_inter_sms_h[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==28 + 35 * cmpt_NumAccount)
		            		conso_inter_mms[cmpt_NumAccount] = tag.getText().toString();
		            	if(cmpt==29 + 35 * cmpt_NumAccount)
		            		conso_inter_mms_h[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==31 + 35 * cmpt_NumAccount)
		            		conso_inter_data[cmpt_NumAccount] = "";
		            	if(cmpt==31 + 35 * cmpt_NumAccount)
		            		conso_inter_data_h[cmpt_NumAccount] = tag.getText().toString();
		            	
		            	if(cmpt==32 + 35 * cmpt_NumAccount)
		            		total_forfait[cmpt_NumAccount] = tag.getText().toString().trim();
		            	if(cmpt==33 + 35 * cmpt_NumAccount)
		            		total_forfait_h[cmpt_NumAccount] = tag.getText().toString().trim();
		            	if(cmpt==34 + 35 * cmpt_NumAccount) {
		            		total[cmpt_NumAccount] = tag.getText().toString().trim();
		            		cmpt_NumAccount++;
		            	}
		            	cmpt++;
		            }
		        }
		        return true;
			}
		});
	}

	public String getName(int i) {
		return name[i];
	}
	
	public int getNumberNames() {
		for(int i=0; i < name.length ; i++) {
			if(name[i]==null)
				return i;
		}
		return 0;
	}
	
	public String[] getNames() {
		return name;
	}

	public void setName(String name, int i) {
		this.name[i] = (String) name;
	}

	public String getDates(int i) {
		return dates[i];
	}

	public void setDates(String dat, int i) {
		this.dates[i] = dat;
	}

	public String getConso_voix(int i) {
		return conso_voix[i];
	}

	public void setConso_voix(String conso_v, int i) {
		this.conso_voix[i] = conso_v;
	}

	public String getConso_voix_h(int i) {
		return conso_voix_h[i];
	}

	public void setConso_voix_h(String conso_v_h, int i) {
		this.conso_voix_h[i] = conso_v_h;
	}

	public String getConso_voix_num_spec(int i) {
		return conso_voix_num_spec[i];
	}

	public void setConso_voix_num_spec(String conso_v_num_spec, int i) {
		this.conso_voix_num_spec[i] = conso_v_num_spec;
	}

	public String getConso_sms(int i) {
		return conso_sms[i];
	}

	public void setConso_sms(String conso_sms, int i) {
		this.conso_sms[i] = conso_sms;
	}

	public String getConso_sms_h(int i) {
		return conso_sms_h[i];
	}

	public void setConso_sms_h(String conso_sms_h, int i) {
		this.conso_sms_h[i] = conso_sms_h;
	}

	public String getConso_mms(int i) {
		return conso_mms[i];
	}

	public void setConso_mms(String conso_mms, int i) {
		this.conso_mms[i] = conso_mms;
	}

	public String getConso_mms_h(int i) {
		return conso_mms_h[i];
	}

	public void setConso_mms_h(String conso_mms_h, int i) {
		this.conso_mms_h[i] = conso_mms_h;
	}

	public String getConso_data(int i) {
		return conso_data[i];
	}

	public void setConso_data(String conso_data, int i) {
		this.conso_data[i] = conso_data;
	}

	public String getConso_data_h(int i) {
		return conso_data_h[i];
	}

	public void setConso_data_h(String conso_data_h, int i) {
		this.conso_data_h[i] = conso_data_h;
	}

	public String getTotal_forfait(int i) {
		return total_forfait[i];
	}

	public void setTotal_forfait(String total_forfait, int i) {
		this.total_forfait[i] = total_forfait;
	}

	public String getTotal_forfait_h(int i) {
		return total_forfait_h[i];
	}

	public void setTotal_forfait_h(String total_f_h, int i) {
		this.total_forfait_h[i] = total_f_h;
	}

	public String getTotal(int i) {
		return total[i];
	}

	public void setTotal(String total, int i) {
		this.total[i] = total;
	}

	public void setImagesBitmap(LinkedList<Bitmap> imagesBitmap) {
		this.imagesBitmap = imagesBitmap;
	}

	public LinkedList<Bitmap> getImagesBitmap() {
		return imagesBitmap;
	}
	
	public String getConso_inter_voix(int i) {
		return conso_inter_voix[i];
	}

	public void setConso_inter_voix(String conso_inter_voix, int i) {
		this.conso_inter_voix[i] = conso_inter_voix;
	}

	public String getConso_inter_voix_h(int i) {
		return conso_inter_voix_h[i];
	}

	public void setConso_inter_voix_h(String conso_inter_voix_h, int i) {
		this.conso_inter_voix_h[i] = conso_inter_voix_h;
	}

	public String getConso_inter_sms(int i) {
		return conso_inter_sms[i];
	}

	public void setConso_inter_sms(String conso_inter_sms, int i) {
		this.conso_inter_sms[i] = conso_inter_sms;
	}

	public String getConso_inter_sms_h(int i) {
		return conso_inter_sms_h[i];
	}

	public void setConso_inter_sms_h(String conso_inter_sms_h, int i) {
		this.conso_inter_sms_h[i] = conso_inter_sms_h;
	}

	public String getConso_inter_mms(int i) {
		return conso_inter_mms[i];
	}

	public void setConso_inter_mms(String conso_inter_mms, int i) {
		this.conso_inter_mms[i] = conso_inter_mms;
	}

	public String getConso_inter_mms_h(int i) {
		return conso_inter_mms_h[i];
	}

	public void setConso_inter_mms_h(String conso_inter_mms_h, int i) {
		this.conso_inter_mms_h[i] = conso_inter_mms_h;
	}

	public String getConso_inter_data(int i) {
		return conso_inter_data[i];
	}

	public void setConso_inter_data(String conso_inter_data, int i) {
		this.conso_inter_data[i] = conso_inter_data;
	}

	public String getConso_inter_data_h(int i) {
		return conso_inter_data_h[i];
	}

	public void setConso_inter_data_h(String conso_inter_data_h, int i) {
		this.conso_inter_data_h[i] = conso_inter_data_h;
	}

	public ArrayList<String> getFactures() {
		return factures;
	}

	public void setFactures(ArrayList<String> factures) {
		this.factures = factures;
	}

	public String getTel(int i) {
		return tel[i];
	}

	public void setTel(String tele, int i) {
		this.tel[i] = tele;
	}

	public void setImagesString(LinkedList<String> imagesString) {
		this.imagesString = imagesString;
	}

	public LinkedList<String> getImagesString() {
		return imagesString;
	}

	public ArrayList<String> getDateFactures() {
		return factures;
	}
	
	public ArrayList<String> getFacturesCost() {
		return facturesCost;
	}
}
